const { ccclass, property } = cc._decorator;
import bgMountain from './bgMountain';
import airstrip from './airstrip';

@ccclass
export default class Background extends cc.Component {
    @property(cc.Node)
    FlyNode1: cc.Node = null;//漂浮物
    @property(cc.Node)
    FlyNode2: cc.Node = null;//漂浮物
    @property(cc.Node)
    CloudNode: cc.Node = null;//云
    @property(cc.Node)
    MountainNode1: cc.Node = null;//山脉
    @property(cc.Node)
    MountainNode2: cc.Node = null;//山脉
    @property(cc.Node)
    MountainNode3: cc.Node = null;//山脉
    @property
    MountainSpeed: number = 0;//山脉移动系数
    fly: cc.SpriteAtlas = null;
    Clouds: cc.SpriteAtlas = null;
    bg: cc.SpriteAtlas = null;

    onLoad() {
        this.setBg();
    }

    init() {
        this.setMountain();
        this.setCloud();
        this.setFly();
    }

    setBg() {
        this.node.getComponent(cc.Sprite).spriteFrame = this.bg.getSpriteFrame("sky");
    }
    setMountain() {
        let bg1 = this.getSprites("c");
        if (bg1?.length > 0)
            this.setBgM(this.MountainNode1, 170, this.MountainSpeed / 3, bg1);
        let bg2 = this.getSprites("b");
        if (bg2?.length > 0)
            this.setBgM(this.MountainNode2, 100, this.MountainSpeed / 2, bg2);
        let bg3 = this.getSprites("a");
        if (bg3?.length > 0)
            this.setBgM(this.MountainNode3, 0, this.MountainSpeed, bg3);
    }
    private setBgM(par: cc.Node, h: number, speed: number, bgs: cc.SpriteFrame[]) {
        let node = new cc.Node();
        node.anchorY = par.anchorY;
        let control = node.addComponent(bgMountain);
        control.bg = bgs;
        control.MountainSpeed = speed;
        node.y = h;
        par.addChild(node);
        control.init();
    }
    setCloud() {
        let clousd = this.getSprites("d");
        if (clousd) {
            let control = this.CloudNode.addComponent(bgMountain);
            control.bg = clousd;
            control.MountainSpeed = 0;
            control.init();
        }
    }
    setFly() {
        let node1 = new cc.Node();
        let sprite1 = node1.addComponent(cc.Sprite);
        let _fly1 = this.fly.getSpriteFrames();
        let _anim1 = node1.addComponent(cc.Animation);
        let clip1 = cc.AnimationClip.createWithSpriteFrames(_fly1, _fly1.length);
        clip1.name = "anim";
        clip1.wrapMode = cc.WrapMode.Loop;
        _anim1.addClip(clip1);
        let control1 = node1.addComponent(airstrip);
        control1.scaleTo = 0.5;
        control1.interval = cc.v2(0, 10);
        control1.speed = cc.v2(100, 200);
        control1.isAnim = true;
        this.FlyNode1.addChild(node1);

        let node2 = new cc.Node();
        let sprite2 = node2.addComponent(cc.Sprite);
        let _fly2 = this.fly.getSpriteFrames();
        let _anim2 = node1.addComponent(cc.Animation);
        let clip = cc.AnimationClip.createWithSpriteFrames(_fly2, Math.floor(_fly2.length * 1.5));
        clip.name = "anim";
        clip.wrapMode = cc.WrapMode.Loop;
        _anim2.addClip(clip);
        let control2 = node2.addComponent(airstrip);
        control2.interval = cc.v2(10, 30);
        control2.speed = cc.v2(200, 300);
        control2.isAnim = true;
        this.FlyNode2.addChild(node2);
    }
    private getSprites(name: string): cc.SpriteFrame[] {
        let sprits: Array<cc.SpriteFrame> = new Array<cc.SpriteFrame>();
        let bgs = this.bg.getSpriteFrames();
        for (let i = 0; i < bgs.length; i++) {
            if (name == bgs[i].name.charAt(0))
                sprits.push(bgs[i]);
        }
        return sprits;
    }
}
